home *** CD-ROM | disk | FTP | other *** search
-
- #include "SampleDefs.h" /* C structures we define */
- #include "Sample.h" /* defines common to C and Rez */
- #include "SampleErrors.h"
- #include "SampleMain.h"
- #include "SampleFileIO.h"
-
- #include <Errors.h>
- #include <Memory.h>
- #include <Aliases.h>
- #include <ToolUtils.h>
- #include <Script.h>
- #include <Finder.h>
- #include <Resources.h>
-
-
- #pragma segment Main
- /************************************************************************************
-
- ConvertOldToNewSFReply
-
- struct StandardFileReply { struct SFReply {
- Boolean sfGood; <- Boolean good;
- Boolean sfReplacing; <- Boolean copy;
- OSType sfType; <- OSType fType;
- FSSpec sfFile;
- vRefNum; <- real vRefnum from (short vRefNum)
- parID; <- real dirID from (short vRefNum)
- name; <- Str63 fName;
- ScriptCode sfScript; <- smSystemScript
- short sfFlags; <- 0
- Boolean sfIsFolder; <- false
- Boolean sfIsVolume; <- false
- long sfReserved1; <- 0
- short sfReserved2; <- 0
- }; };
-
- *************************************************************************************/
- void ConvertOldToNewSFReply(SFReply *oldReply, StandardFileReply *newReply)
- {
- OSErr err;
- long ignoredProcID;
-
- newReply->sfGood = oldReply->good;
- newReply->sfReplacing = oldReply->copy; /* correct assignment? */
- newReply->sfType = oldReply->fType;
-
- err = GetWDInfo(oldReply->vRefNum,
- &newReply->sfFile.vRefNum,
- &newReply->sfFile.parID,
- &ignoredProcID);
- BlockMove((Ptr) &oldReply->fName, (Ptr) &newReply->sfFile.name, oldReply->fName[0]+1);
-
- /* punt on the rest */
- newReply->sfScript = smSystemScript;
- newReply->sfFlags = 0;
- newReply->sfIsFolder = false;
- newReply->sfIsVolume = false;
- newReply->sfReserved1 = 0;
- newReply->sfReserved2 = 0;
- }
-
-
- #pragma segment Main
- /************************************************************************************
-
- WriteWindowData
-
- Writes the data in gWindowData to the file referenced by "refnum". The file is
- closed after the data's been written.
-
- *************************************************************************************/
- OSErr WriteWindowData(short refNum)
- {
- long count = sizeof(WindowData);
- OSErr err;
-
- if (err = FSWrite(refNum, &count, (Ptr) &gWindowData))
- DisplayError(err, nil, "In WriteWindowData, after FSWrite.");
- if (err = FSClose(refNum))
- DisplayError(err, nil, "In WriteWindowData, after FSClose.");
- return err;
- }
-
- #pragma segment Main
- /************************************************************************************
-
- ReadWindowData
-
- Reads the data from the file referenced by "refnum" into gWindowData. The file is
- closed after the data's been read.
-
- *************************************************************************************/
- OSErr ReadWindowData(short refNum)
- {
- long count = sizeof(WindowData);
- OSErr err;
-
- if (err = FSRead(refNum, &count, (Ptr) &gWindowData))
- DisplayError(err, nil, "In ReadWindowData, after FSRead.");
- if (err = FSClose(refNum))
- DisplayError(err, nil, "In ReadWindowData, after FSClose.");
-
- return err;
- }
-
- #pragma segment Main
- /************************************************************************************
-
- Create_OpenFile
-
- Opens the file specified by the passed FSSpec, creating it if it doesn't
- already exist. Refturns the refnum of the open file to the application.
- File Manager errors are reported and returned.
-
- *************************************************************************************/
- OSErr Create_OpenFile(FSSpec *file, short *refNum)
- {
- OSErr err;
-
- if ((err = HCreate(file->vRefNum, file->parID, file->name, 'DTS ', 'BOOL')) == dupFNErr) {
-
- /* the user already told Standard File to replace the old file
- so let's get rid of it */
-
- if (err = HDelete(file->vRefNum, file->parID, file->name))
- DisplayError(err, nil, "In Create_OpenFile, after HDelete.");
-
- /* try again */
- else if (err = HCreate(file->vRefNum, file->parID, file->name, 'DTS ', 'BOOL'))
- DisplayError(err, nil, "In Create_OpenFile, after HCreate (again).");
- }
-
- if ( !err )
- err = HOpen(file->vRefNum, file->parID, file->name, fsRdWrPerm, refNum);
- if (err)
- DisplayError(err, nil, "In Create_OpenFile, after HOpen.");
-
- return err;
- }
-
- #pragma segment Main
- /************************************************************************************
-
- DisplayPutFile
-
- Displays the StandardFile PutFile dialog box. Fills out the passed reply
- record, and returns the sfGood field as a result.
-
- *************************************************************************************/
- Boolean DisplayPutFile(StandardFileReply *reply)
- {
- Str255 prompt;
- Str255 origName;
- Point where = {100, 100};
- SFReply oldReply;
-
- GetIndString(prompt,kMiscStrings,kSFprompt);
- GetIndString(origName,kMiscStrings,korigName);
-
- if (gHasNewStdFile)
- StandardPutFile(prompt, origName, reply);
- else {
- SFPutFile(where, prompt, origName, nil, &oldReply);
- ConvertOldToNewSFReply(&oldReply, reply);
- }
-
- return reply->sfGood;
- }
-
- #pragma segment Main
- /************************************************************************************
-
- SaveFile
-
- Given a window pointer, this routine saves its state to disk. We first ask
- the user for the name of a file to write to. If the user doesn't press
- Cancel, we create the file (or overwrite an existing one), write the data
- to it, and close the file. We also change the name of the window to be the
- same as the file we just created.
-
- *************************************************************************************/
- Boolean SaveFile(WindowPtr window)
- {
- StandardFileReply reply;
- short refNum;
- OSErr err;
-
- if ( DisplayPutFile(&reply) ) {
- ExtractWindowData(window);
- if (err = Create_OpenFile(&reply.sfFile, &refNum))
- DisplayError(err, nil, "In SaveFile, after Create.");
- else if (err = WriteWindowData(refNum))
- DisplayError(err, nil, "In SaveFile, after Write.");
-
- SetWTitle(window, reply.sfFile.name);
- }
-
- return reply.sfGood;
- }
-
-
- #pragma segment Main
- /************************************************************************************
-
- DisplayGetFile
-
- Simple routine to display a list of files with our file type.
-
- *************************************************************************************/
- Boolean DisplayGetFile(StandardFileReply *reply)
- {
- SFTypeList typeList = {'BOOL'};
- Point where = {100, 100};
- SFReply oldReply;
-
- if (gHasNewStdFile)
- StandardGetFile(nil, 1, typeList, reply);
- else {
- SFGetFile(where, "\pSelect a document to open.", nil, 1, typeList, nil, &oldReply);
- ConvertOldToNewSFReply(&oldReply, reply);
- }
-
- return reply->sfGood;
- }
-
-
- #pragma segment Main
- /************************************************************************************
-
- OpenFile
-
- Given a pointer to an AEDesc, this routine checks to see if it's nil. If so,
- then we bring up a StandardFile dialog box asking for a name. If the pointer
- is NOT nil, we extract from it an FSSpec. In either case, we should end up
- with an FSSpec of a file to open. We open it, read the data from it, and use
- that data to create a new window.
-
- *************************************************************************************/
- OSErr OpenFile(FSSpecPtr fileToOpen)
- {
- OSErr err;
- StandardFileReply reply;
- Boolean GotReply;
- FSSpec myFileSpec;
- short fileRefNum;
-
- FSSpec appleMenuFolder;
- short vRefNum;
- short foundVRefNum;
- long foundDirID;
- AliasHandle alias;
- short fRef;
- FInfo fInfo;
-
-
- if ( !fileToOpen ) {
- if (GotReply = DisplayGetFile(&reply))
- BlockMove((Ptr) &reply.sfFile, (Ptr) &myFileSpec, sizeof(FSSpec));
- }
- else {
- GotReply = true;
- BlockMove((Ptr) fileToOpen, (Ptr) &myFileSpec, sizeof(FSSpec));
- }
-
- if ( GotReply ) { // we shouldn't be doing this everytime we open a file, but it's late & it's a hack!
- err = FindFolder(-1,kAppleMenuFolderType,false,&foundVRefNum, &foundDirID);
- if (!err)
- {
- err = NewAlias(nil,&myFileSpec,&alias);
- }
-
- if (!err)
- {
- HCreateResFile(foundVRefNum, foundDirID, myFileSpec.name);
- err = ResError();
- }
-
- if (!err)
- {
- fRef = HOpenResFile(foundVRefNum, foundDirID, myFileSpec.name, fsRdWrPerm);
- err = ResError();
- }
-
- if (!err)
- {
- AddResource( (Handle)alias, 'alis', 0,myFileSpec.name);
- err = ResError();
- }
-
- if (!err)
- {
- CloseResFile(fRef);
- err = ResError();
- }
-
- if (!err)
- {
- err = FSpGetFInfo(&myFileSpec, &fInfo);
- }
-
- if (!err)
- {
- fInfo.fdFlags |= 1<<15;
- fInfo.fdFlags &= ~(1<<8);
- err = HSetFInfo(foundVRefNum, foundDirID, myFileSpec.name, &fInfo);
- }
-
- gQuit = true;
- }
- return(err);
- }
-